﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using blackhouse.WebNemo.Data;
using Wintellect.PowerCollections;

namespace blackhouse.WebNemo.Business.Groups {
    public class VirtualColumns {

        #region Fields

        private IDataAccess dataAccess = Factory.Instance().GetDataAccess();
        private Dictionary<int, VirtualColumn> virtualColumns = new Dictionary<int, VirtualColumn>();
        private Dictionary<int, int> relColumns = new Dictionary<int, int>();

        #endregion

        #region Constructors

        public VirtualColumns(int groupId) {
            VirtualColumnData[] data = this.dataAccess.GetVirtualColumns(groupId);
            foreach (VirtualColumnData columnData in data) {
                VirtualColumn column = new VirtualColumn(columnData);
                virtualColumns.Add(column.VirtualId, column);
                foreach (int columnId in column.VirtualizeColumnsId) {
                    relColumns.Add(columnId, column.VirtualId);
                }
            }
        }

        #endregion

        #region Methods

        public bool IsVirtualizationColumn(GroupColumn column) {
            return this.relColumns.ContainsKey(column.ColumnId);
        }

        public VirtualColumn GetVirtualColumn(GroupColumn column) {
            if (this.relColumns.ContainsKey(column.ColumnId)) {
                int vcId = this.relColumns[column.ColumnId];
                if (this.virtualColumns.ContainsKey(vcId))
                    return this.virtualColumns[vcId];
            }
            return null;
        }

        #endregion

    }
}
